package {
	import cn.va81.component.MaoMao;
	import cn.va81.component.MaoType;
	import cn.va81.component.TestDrag;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class TestMaoMao extends Sprite
	{
		
		private var output:TextField;
		
		public function TestMaoMao()
		{
			init();
		}
		
		private function init():void
		{
			var main:Shape = new Shape();
			var g:Graphics = main.graphics;
			g.beginFill(0xffffff,1);
			g.drawRect(5,5,360,360);
			g.endFill();
			addChild(main);
//			for(var i:int=0;i<6;i++)
//			{
//				for(var j:int=0;j<6;j++)
//				{
//					var redMM:MaoMao = new MaoMao(i,j,0xFF0033,30,MaoType.COMMON,false,10);
//					var redMM:MaoMao = new MaoMao(2,3,0xFF0033,30,MaoType.COMMON,false,10);
//					redMM.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
//					redMM.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
//					addChild(redMM);
//				}
//			}
			var td:TestDrag = new TestDrag();
			td.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
			td.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			addChild(td);
			output = new TextField();
			output.x = 450;
			output.y = 5;
			addChild(output);
		}
		
		private function handleMouseDown(e:MouseEvent):void
		{
//			e.target.startDrag();
//			e.target.addEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
//			output.text = "";
			e.target.drag();
		}
		
		private function handleMouseMove(e:MouseEvent):void
		{
			var orgX:uint = e.target.getX();
			var orgY:uint = e.target.getY();
			var nowX:uint = e.stageX;
			var nowY:uint = e.stageY;
			output.text = orgX+"//"+orgY+"\n"+e.target.getX()+"//"+e.target.getY()+"\n"+nowX+"//"+nowY;
			if(Math.abs(orgX-nowX)>Math.abs(orgY-nowY))
			{
				if(orgX-nowX>0)
				{
					e.target.x += 60;
					e.target.setX(e.target.getX()+1);
					//output.text = "1";
				}
				else
				{
					e.target.x -= 60;
					e.target.setX(e.target.getX()-1);
					//output.text = "2";
				}
			}
			else
			{
				if(orgY-nowY>0)
				{
					e.target.y += 60;
					e.target.setY(e.target.getY()+1);
//					output.text = "3";
				}
				else
				{
					e.target.y -= 60;
					e.target.setY(e.target.getY()-1);
//					output.text = "4";
				}
			}
		}
		
		private function handleMouseUp(e:MouseEvent):void
		{
//			e.target.stopDrag();
//			e.target.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove,false);
//			output.text = "";
			e.target.drop();
		}
	}
}
